package com.yanghui.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.pojo.Resource;
import com.yanghui.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin")
@Controller
public class ResourceControllerAdmin {
    @Autowired
    private ResourceService resourceService;


    /**
     * 添加资源记录
     */
    @RequestMapping("/addResource")
    @ResponseBody
    public Map<String,Object> addResource(Resource resource) {
        Map<String,Object> map = new HashMap<>();
        int count = resourceService.addResource(resource);

        if (count == 1) {
            map.put("status", 200);
            map.put("msg", "添加资源成功！");
        } else {
            map.put("status", 500);
            map.put("msg", "添加资源失败，服务器内部错误！");
        }

        return map;
    }


    /**
     * 按照ID删除某条资源信息
     */
    @RequestMapping("/deleteResourceById")
    @ResponseBody
    public Map<String,Object> deleteResourceById(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        int count = resourceService.deleteResourceById(id);

        if (count == 1) {
            map.put("status", 200);
            map.put("msg", "删除成功！");
        } else {
            map.put("status", 500);
            map.put("msg", "删除失败，服务器内部错误！");
        }

        return map;
    }

    /**
     * 按照Id修改资源信息
     */
    @RequestMapping("/updateResourceById")
    @ResponseBody
    public Map<String,Object> updateResourceById(Resource resource) {
        Map<String,Object> map = new HashMap<>();
        /** 返回受影响记录的行数 */
        int count = resourceService.updateResourceById(resource);

        if (count == 1) {
            map.put("status", 200);
            map.put("msg", "修改资源信息成功！");
        } else {
            map.put("status", 500);
            map.put("msg", "服务器内部错误！");
        }

        return map;
    }

    /**
     * 按照id查询资源记录
     */
    @RequestMapping("/selectResourceById")
    @ResponseBody
    public Map<String,Object> selectResourceById(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        Resource resource = resourceService.selectResourceById(id);

        if (resource != null) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("resource", resource);

        return map;
    }

    /**
     * 按照查询条件查询资源记录
     * @param resource
     * @param page      页码
     * @param pageSize  页面大小
     */
    @RequestMapping("/selectLikeResourceListByPage")
    @ResponseBody
    public Map<String,Object> selectLikeResourceListByPage(
            Resource resource,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        /** 封装查询条件 */
        Map<String,Object> map = new HashMap<>();
        // 标题
        if (resource.getTitle() != null && "".equals(resource.getTitle()) != true) {
            map.put("title", "%" + resource.getTitle() + "%");
        }
        // 内容
        if (resource.getContent() != null && "".equals(resource.getContent()) != true) {
            map.put("content", "%" + resource.getContent() + "%");
        }
        // 状态
        if (resource.getStatus() != null && "".equals(resource.getTitle()) != true) {
            map.put("status", resource.getStatus());
        }

        /**
         * 查询结果，使用 PageHelper 插件进行分页
         */
        Map<String,Object> resultMap = new HashMap<>();
        PageHelper.startPage(page, pageSize);
        List<Resource> resourceList = resourceService.selectLikeResourceListByPage(map);
        PageInfo pageInfo = new PageInfo(resourceList);
        resultMap.put("pageInfo", pageInfo);

        if (resourceList.size() > 0) {
            resultMap.put("status", 200);
        } else {
            resultMap.put("status", 500);
        }

        return resultMap;
    }

    /**
     * 按审核状态分组查询数量
     */
    @RequestMapping("/selectResourceListByStatus")
    @ResponseBody
    public Map<String,Object> selectResourceListByStatus() {
        Map<String,Object> map = new HashMap<>();
        List<?> list = resourceService.selectResourceListByStatus();

        if (list.size() > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("list", list);

        return map;
    }

}
